package common;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.net.Socket;

public class SocketPerClientInterface implements ClientInterface, Serializable{

	private transient Socket socket;
	private transient BufferedWriter bufferedWriter;
	
	public SocketPerClientInterface(Socket socket){
		this.socket=socket;
		
		try {
			bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public SocketPerClientInterface(){
	
	}
	
	@Override
	public void ok() throws IOException{
		//invio messaggio a client
		String mess="@ok";
		
		bufferedWriter.write(mess);
		bufferedWriter.newLine();
		bufferedWriter.flush();
		System.out.println(socket+" ---> "+mess);
	}

	@Override
	public void ok(String s)throws IOException {
		//invio messaggio a client
		String mess="@ok,"+s;
		System.out.println(socket+" ---> "+mess);
		bufferedWriter.write(mess);
		bufferedWriter.newLine();
		bufferedWriter.flush();
	}

	@Override
	public void no(String errore) throws IOException{
		//invio messaggio a client
		String mess="@no,"+errore;
		System.out.println(socket+" ---> "+mess);
		bufferedWriter.write(mess);
		bufferedWriter.newLine();
		bufferedWriter.flush();
	}

	@Override
	public void listaGiocatori(String[] giocatori) throws IOException {
		String mess="@listaGiocatori";
		for(int i=0;i<giocatori.length;i++){
				mess=mess+","+giocatori[i];
		}
		bufferedWriter.write(mess);
		bufferedWriter.newLine();
		bufferedWriter.flush();
		System.out.println(socket+" ---> "+mess);
	}

	@Override
	public void classifica(String[] classifica) throws IOException {
		String mess="@classifica";
		for(int i=0;i<classifica.length;i++){
				String[] infoGiocatore=classifica[i].split("[\\:\\s]");
				mess=mess+",{"+infoGiocatore[2]+","+infoGiocatore[6]+","+infoGiocatore[10]+","+infoGiocatore[16]+"}";
				
		}
		System.out.println(mess);
		bufferedWriter.write(mess);
		bufferedWriter.newLine();
		bufferedWriter.flush();
		System.out.println(socket+" ---> "+mess);
		
	}

	@Override
	public void mappaGenerale(String[] mappa) throws IOException {
		String mess="@mappaGenerale,{"+mappa[0]+","+mappa[1]+"},";
		for(int i=2;i<mappa.length;i++){
			for(int j=0;j<mappa[i].length();j++){
				mess=mess+"["+mappa[i].toCharArray()[j]+"]";
			}
			mess=mess+";";
		}
		bufferedWriter.write(mess);
		bufferedWriter.newLine();
		bufferedWriter.flush();
		System.out.println(socket+" ---> "+mess);
		
	}

	@Override
	public void listaDinosauri(String[] dinosauri) throws IOException {
		String mess="@listaDinosauri";
		int len=dinosauri.length;
		if(len>0){
			for(int i=0;i<len;i++){
				mess=mess+","+dinosauri[i];
			}
		}
		bufferedWriter.write(mess);
		bufferedWriter.newLine();
		bufferedWriter.flush();
		System.out.println(socket+" ---> "+mess);
		
	}

	@Override
	public void vistaLocale(String[] vista) throws IOException {
		String mess="@vistaLocale";
		mess=mess+",{"+vista[0]+","+vista[1]+"},{"+vista[2]+","+vista[3]+"},";
		int len=vista.length;
		for(int i=4;i<len;i++){
			String riga=vista[i];
			String[] rigaSep=riga.split("\\;");
			int colonne= rigaSep.length;
			String rigaOK="";
			for(int j=0;j<colonne;j++){
				rigaOK=rigaOK+"["+rigaSep[j]+"]";
			}
			mess=mess+rigaOK+";";
			
		}
		//System.out.println("\nstampo mess server->client\n"+ mess);
		bufferedWriter.write(mess);
		bufferedWriter.newLine();
		bufferedWriter.flush();
		System.out.println(socket+" ---> "+mess);
		
		
	}

	@Override
	public void statoDinosauro(String dinosauro) throws IOException {
		String mess="@statoDinosauro,";
		
		int numInfo=dinosauro.split("\\\n").length;
		String[] info=dinosauro.split("\\\n");
		
		int Lnome=info[0].split("\\:")[1].length();
		String nome=info[0].split("\\:")[1].split("\\s")[1];
		int Lrazza=info[1].split("\\:")[1].length();	
		String razza=info[1].split("\\:")[1].split("\\s")[1];
		int Ltipo=info[2].split("\\:")[1].length();
		String tipo=info[2].split("\\:")[1].split("\\s")[1];
		String posiz=info[3];
		int Ldim=info[4].split("\\:")[1].length();
		String dim=info[4].split("\\:")[1].split("\\s")[1];
		mess=mess+nome+","+razza+","+tipo+","+posiz+","+dim;
		if(numInfo==5){
			
		}
		else{
			int Lenergia=info[5].split("\\:")[1].length();	
			String energia=info[5].split("\\:")[1].split("\\s")[1];
			int Lturni=info[6].split("\\:")[1].length();
			String turni=info[6].split("\\:")[1].split("\\s")[1];
			mess=mess+","+energia+","+turni;
		}
		bufferedWriter.write(mess);
		bufferedWriter.newLine();
		bufferedWriter.flush();
		System.out.println(socket+" ---> "+mess);
		
	}

	@Override
	public void cambioTurno(String username) throws IOException {
		String mess="@cambioTurno,"+username;
		
		bufferedWriter.write(mess);
		bufferedWriter.newLine();
		bufferedWriter.flush();
		System.out.println(socket+" ---> "+mess);
		
	}



}
